package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.R;
import com.example.demo.dto.OrderStatus;
import com.example.demo.dto.OrdersDto;
import com.example.demo.entity.AddressBook;
import com.example.demo.entity.OrderDetail;
import com.example.demo.entity.Orders;
import com.example.demo.entity.User;
import com.example.demo.service.AddressBookService;
import com.example.demo.service.OrderDetailService;
import com.example.demo.service.OrdersService;
import com.example.demo.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * date 2022年07月28日 15:41
 */
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UserService userService;

    @Autowired
    private AddressBookService addressBookService;
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        ordersService.submit(orders);
        return R.success("下单成功");
    }
    @GetMapping("/page")
    public R<Page<OrdersDto>> page(int page, int pageSize, String number,
                                   Date beginTime,
                                   Date endTime) {
        LocalDateTime localDateTimeBegin = null;
        LocalDateTime localDateTimeEnd = null;
        // 对其时间参数进行处理
        if(beginTime !=null && endTime!=null){
            Instant instant = beginTime.toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            localDateTimeBegin = instant.atZone(zoneId).toLocalDateTime();
            // endTime 进行处理
            Instant instant1 = endTime.toInstant();
            ZoneId zoneId1 = ZoneId.systemDefault();
            localDateTimeEnd = instant1.atZone(zoneId1).toLocalDateTime();
        }
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        Page<OrdersDto> pageDto = new Page<>();
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(number)){
            wrapper.eq(Orders::getNumber,number);
        }
        if(!StringUtils.isEmpty(localDateTimeBegin)){
            wrapper.ge(Orders::getOrderTime,localDateTimeBegin);
        }
        if(!StringUtils.isEmpty(localDateTimeEnd)){
            wrapper.le(Orders::getOrderTime,localDateTimeEnd);
        }
        wrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(pageInfo,wrapper);
        // 将其除了records中的内存复制到pageDto中
        BeanUtils.copyProperties(pageInfo,pageDto,"records");
        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> collect = records.stream().map((order -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(order, ordersDto);
            // 根据订单id查询订单详细信息
            LambdaQueryWrapper<OrderDetail> wrapperDetail = new LambdaQueryWrapper<>();
            wrapperDetail.eq(OrderDetail::getOrderId, order.getId());
            List<OrderDetail> orderDetails = orderDetailService.list(wrapperDetail);
            ordersDto.setOrderDetails(orderDetails);
            // 根据userId 查询用户姓名
            Long userId = order.getUserId();
            User user = userService.getById(userId);
            ordersDto.setUserName(user.getName());
            ordersDto.setPhone(user.getPhone());

            // 获取地址信息
            Long addressBookId = order.getAddressBookId();
            AddressBook addressBook = addressBookService.getById(addressBookId);
            ordersDto.setAddress(addressBook.getDetail());
            ordersDto.setConsignee(addressBook.getConsignee());
            return ordersDto;
        })).collect(Collectors.toList());
        pageDto.setRecords(collect);
        return R.success(pageDto);
    }
    @PutMapping
    public R<String> statusOrder(@RequestBody OrderStatus orderStatus) {
        Orders orders = ordersService.getById(orderStatus.getId());
        orders.setStatus(orderStatus.getStatus());
        ordersService.updateById(orders);
        return R.success("派送完成");
    }
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize) {
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        Page<OrdersDto> pageDto = new Page<>();
        ordersService.page(pageInfo,null);
        // 将其除了records中的内存复制到pageDto中
        BeanUtils.copyProperties(pageInfo,pageDto,"records");
        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> collect = records.stream().map((order -> {
            OrdersDto ordersDto = new OrdersDto();
            // 根据订单id查询订单详细信息
            LambdaQueryWrapper<OrderDetail> wrapperDetail = new LambdaQueryWrapper<>();
            wrapperDetail.eq(OrderDetail::getOrderId, order.getId());
            List<OrderDetail> orderDetails = orderDetailService.list(wrapperDetail);
            ordersDto.setOrderDetails(orderDetails);
            Long userId = order.getUserId();
            User user = userService.getById(userId);
            ordersDto.setUserName(user.getName());
            ordersDto.setPhone(user.getPhone());
            // 获取地址信息
            Long addressBookId = order.getAddressBookId();
            AddressBook addressBook = addressBookService.getById(addressBookId);
            ordersDto.setAddress(addressBook.getDetail());
            ordersDto.setConsignee((addressBook.getConsignee()));
            return ordersDto;
        })).collect(Collectors.toList());
        pageDto.setRecords(collect);
        return R.success(pageDto);
    }
}
